package com.escope.casemanager.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.escope.casemanager.dao.SysUserDao;
import com.escope.casemanager.exception.MsgException;
import com.escope.casemanager.model.HttpResult;
import com.escope.casemanager.model.SysUser;
import com.escope.casemanager.model.SysUserCacheInfo;
import com.escope.casemanager.service.SysUserService;
import com.escope.casemanager.uti.CookiesUtil;
import com.escope.casemanager.uti.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * Administrator
 * 2024/3/15
 */
@Transactional(rollbackFor = Exception.class)
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private SysUserDao sysUserDao;


    @Override
    public HttpResult accLogin(String userCode, String password, HttpServletResponse response) {
        if (StringUtils.isEmpty(userCode) || StringUtils.isEmpty(password)) {
            return HttpResult.error("用户名密码不能为空");
        }
        SysUser u = sysUserDao.findSysUserByUserCodeAndPassword(userCode, password);
        if (u != null) {
            SysUserCacheInfo reuslt = new SysUserCacheInfo();
            reuslt.setV1(1);
            reuslt.setUserCode(userCode);
            reuslt.setLogintime(new Date().getTime());
            reuslt.setToken(UUID.randomUUID().toString());

            // 写入redis
            SysUserCacheInfo redisInfo = new SysUserCacheInfo();
            redisInfo.setUser(u);
            redisInfo.setV1(1);
            redisInfo.setUserCode(userCode);
            redisInfo.setLogintime(new Date().getTime());
            redisInfo.setToken(UUID.randomUUID().toString());
            redisUtil.setCacheUser(redisInfo);

            //向浏览器写入Cookies
            CookiesUtil.WriteCookieMap(response, 30*60, (JSONObject) JSONObject.toJSON(reuslt));

            return HttpResult.ok(reuslt);
        } else {
            return HttpResult.error("您的用户名或者密码不对");
        }
    }

    @Override
    public void logout() {
        redisUtil.clearCacheUser();
    }

    @Override
    public SysUser findById(Long userId) {
        return sysUserDao.getOne(userId);
    }

    @Override
    public void updatePassword(Long userId, String oldPassword, String newPassword) throws MsgException {
        if (StringUtils.isEmpty(userId)) {
            throw new MsgException("账号信息为空");
        }
        if (StringUtils.isEmpty(oldPassword)) {
            throw new MsgException("旧密码不能为空");
        }
        if (StringUtils.isEmpty(newPassword)) {
            throw new MsgException("新密码不能为空");
        }
        if (newPassword.length() < 6) {
            throw new MsgException("新密码长度不能小于六位数");
        }

        SysUser u = sysUserDao.getOne(userId);
        if (!oldPassword.equals(u.getPassword())) {
            throw new MsgException("账号密码不匹配！");
        }

        u.setPassword(newPassword);
        sysUserDao.save(u);
    }
}
